package utilities;

import gameAppLayer.enums.Constants;

import java.util.ArrayList;

import com.jme3.math.Vector2f;

/*
 * General utility class for converting coordinates from one system to another
 */
public final class CoordConvert {
	
	public static float s = Constants.hexSideLength;
	
	public static ArrayList<Double> hex2Cart(ArrayList<Integer> hex) {
		
		int u = hex.get(0);
		int v = hex.get(1);
		int w = hex.get(2);
		
		ArrayList<Double> cart = hex2Cart(u, v, w);
		
		return cart;
	}
	
	public static ArrayList<Double> hex2Cart(int u, int v, int w) {
		
		//convert to double, since handling ints will automatically round the calculations
		double uu = u;
		double vv = v;
		double ww = w;
		
		double y = 1.5*ww*s;  //w/(2*Math.tan(Math.PI/6));//1.5*s*v;
		double x = Math.sqrt(3)*s*(w*0.5+u);//ww/2+uu;  //-v;//Math.sqrt(3)*s*(v*0.5+w);
		
		ArrayList<Double> cart = new ArrayList<Double>();
		cart.add(x);
		cart.add(y);
		
		return cart;
	}
	
	public static Vector2f hex2CartVec(ArrayList<Integer> hex) {
		int u = hex.get(0);
		int v = hex.get(1);
		int w = hex.get(2);
		
		ArrayList<Double> cart = hex2Cart(u, v, w);
		
		return new Vector2f(cart.get(0).floatValue(), cart.get(1).floatValue());
	}
	
	
	public static ArrayList<Double> cart2Hex(ArrayList<Double> cart) {
		
		double x = cart.get(0);
		double y = cart.get(1);
		
		ArrayList<Double> hex = cart2Hex(x,y);
		
		return hex;
	}
	
//	public static ArrayList<Integer> cart2HexTile(ArrayList<Integer> cart) {
//		int x = cart.get(0);
//		int y = cart.get(1);
//		
//		ArrayList<Double> hex = cart2Hex(x,y);
//		
//		ArrayList<Integer> hexInt = new ArrayList<Integer>();
//		
//		for (Double value: hex) {
//			hexInt.add((int) Math.round(value)); 
//		}		
//		return hexInt;
//	}
	
	public static ArrayList<Integer> cart2HexTile(ArrayList<Double> cart) {
		
		double x = cart.get(0);
		double y = cart.get(1);
		
		ArrayList<Double> hex = cart2Hex(x,y);
		
		ArrayList<Integer> hexInt = new ArrayList<Integer>();
		//round hex coords
		for (Double value: hex) {
			hexInt.add((int) Math.round(value)); 
		}		
		return hexInt;
		
	}
	
	
	public static ArrayList<Double> cart2Hex(double x, double y) {
		
//		double u = (x/3 - Math.sqrt(3)*y/9)*3;
//		double v = -(x/3 + Math.sqrt(3)*y/9)*3;
//		double w = -(v+u);
		
		double u = (Math.sqrt(3)*x/3 - y/3)/s;
		double v = -(Math.sqrt(3)*x/3 + y/3)/s;
		double w = -(v+u);
		
//		double v = -x;
//		double w = 2*y*Math.tan(Math.PI/6);
//		double u = -(v+w);
		
		ArrayList<Double> hex = new ArrayList<Double>();
		hex.add(u);
		hex.add(v);
		hex.add(w);
		
		return hex;		
	}
	

}
